<template>
  <div>
    <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable :close-on-click-modal="false"
      :close-on-press-escape="false" width="669px">
      <el-form ref="formRef" :model="form" size="default" label-width="80px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="名称" prop="blockName"
              :rules="[{ required: false, message: '请输入名称', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.blockName" placeholder="请输入数据块名称" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="地址" prop="address"
              :rules="[{ required: true, message: '请输入地址', trigger: ['blur', 'change'] }]">
              <el-input type="number" min="0" v-model="form.address" placeholder="请输入地址" autocomplete="off" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="用途" prop="purpose"
              :rules="[{ required: true, message: '请选择数据块用途', trigger: ['blur', 'change'] }]">
              <el-select v-model="form.purpose" placeholder="请选择点位用途" class="w100">
                <el-option v-for="item in porpuseOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="所属" prop="blockType"
              :rules="[{ required: true, message: '请选择数据块服务对象', trigger: ['blur', 'change'] }]">
              <el-select v-model="form.blockType" placeholder="请选择数据块服务对象" class="w100">
                <el-option v-for="item in dataBlockTypeOptions" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="起始偏移" prop="address"
              :rules="[{ required: true, message: '请输入起始偏移量', trigger: ['blur', 'change'] }]">
              <el-input type="number" min="0" v-model="form.startOffset" placeholder="请输入起始偏移量" autocomplete="off"
                clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="地址长度" prop="dataSize"
              :rules="[{ required: true, message: '请输入数据块长度', trigger: ['blur', 'change'] }]">
              <el-input type="number" min="0" v-model="form.dataSize" placeholder="请输入数据块长度" autocomplete="off"
                clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="排序" prop="sort"
              :rules="[{ required: false, message: '排序', trigger: ['blur', 'change'] }]">
              <el-input-number v-model="form.sort" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="描述" prop="dataSize"
              :rules="[{ required: false, message: '请输入描述', trigger: ['blur', 'change'] }]">
              <el-input type="textarea" min="0" v-model="form.description" placeholder="请输入描述" autocomplete="off"
                clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer my-flex my-flex-y-center my-flex-between">
          <div>
            <el-checkbox v-if="!(state.form?.id > 0)" v-model="state.contiAdd">连续新增</el-checkbox>
          </div>
          <div>
            <el-button @click="onCancel" size="default">取 消</el-button>
            <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
          </div>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="industry/station-block-form">
import { reactive, toRefs, getCurrentInstance, ref } from 'vue'
import { StationBlockGetListOutput, PLCBlockAddInput, PLCBlockUpdateInput } from '/@/api/industry/data-contracts'
import { PlcBlockApi } from '/@/api/industry/PlcBlock';
import eventBus from '/@/utils/mitt'

defineProps({
  title: {
    type: String,
    default: '',
  },
})


const porpuseOptions = [
  { id: 1, name: '工艺采集' },
  { id: 2, name: '告警监控' },
  { id: 3, name: '设备配方' }
]

const dataBlockTypeOptions = [
  { id: 1, name: 'PLC' },
  { id: 2, name: 'MES' }
]

const { proxy } = getCurrentInstance() as any
const formRef = ref()
const state = reactive({
  showDialog: false,
  sureLoading: false,
  form: {} as PLCBlockAddInput & PLCBlockUpdateInput,
  contiAdd: false,
})
const { form } = toRefs(state)


// 打开对话框
const open = async (row: any = {}) => {
  if (row.id > 0) {
    state.contiAdd = false
    const res = await new PlcBlockApi().get({ id: row.id }, { loading: true }).catch(() => {
      proxy.$modal.closeLoading()
    })

    if (res?.success) {
      state.form = res.data as PLCBlockAddInput & PLCBlockUpdateInput
    }
  } else {
    state.form = { workStationId: row.stationId } as PLCBlockAddInput & PLCBlockUpdateInput
  }
  state.showDialog = true
}

// 确定
const onSure = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) return

    state.sureLoading = true
    let res = {} as any
    if (state.form.id != undefined && state.form.id > 0) {
      res = await new PlcBlockApi().update(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    } else {
      res = await new PlcBlockApi().add(state.form, { showSuccessMessage: true }).catch(() => {
        state.sureLoading = false
      })
    }
    state.sureLoading = false

    if (res?.success) {
      if (state.contiAdd) {
        formRef.value!.resetFields()
      }
      eventBus.emit('refreshPLCBlock')
      state.showDialog = state.contiAdd
    }
  })
}

// 取消
const onCancel = () => {
  state.showDialog = false
}
defineExpose({
  open,
})
</script>